/*
 * 功能：负责处理与汽车相关的HTTP请求
 * 作者：张杨(Sunny)
 * 日期： 2024/11/6 下午2:36
 */
package com.example.springboot.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Car;
import com.example.springboot.service.CarService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 车辆控制器，提供车辆相关的CRUD（创建读取更新删除）操作
 */
@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    CarService carService;

    // 添加汽车信息
    @PostMapping("/add")
    public Result add(@RequestBody Car car) {
        carService.save(car);
        return Result.success();
    }

    // 更新汽车信息
    @PutMapping("/update")
    public Result update(@RequestBody Car car) {
        carService.updateById(car);
        return Result.success();
    }

    // 根据 id 单个删除汽车信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        carService.removeById(id);
        return Result.success();
    }

    // 根据 ids 批量删除汽车信息
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        carService.removeBatchByIds(ids);
        return Result.success();
    }

    // 根据 id 查询汽车信息
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Car car = carService.getById(id);
        return Result.success(car);
    }

    // 查询所有车辆信息
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Car> carList = carService.list(new QueryWrapper<Car>().orderByDesc("state"));
        return Result.success(carList);
    }

    // 分页查询汽车信息，并支持根据多个条件进行筛选
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String remark, // 备注
                               @RequestParam String number, // 车牌号
                               @RequestParam String brand, // 品牌
                               @RequestParam String model, // 型号(手动档、自动档)
                               @RequestParam String state) { // 状态（可预约、维护中、不可用）
        Page<Car> page = carService.selectByPage(pageNum, pageSize, remark, number, brand, model, state);
        return Result.success(page);
    }

    //导出信息到本地 Excel 文件
    @AuthAccess
    @GetMapping("/export")
    public void exportData(@RequestParam(required = false) String number,
                           @RequestParam(required = false) String remark,
                           @RequestParam(required = false) String ids,
                           HttpServletResponse response) throws IOException {
        carService.exportData(number, remark, ids, response);
    }

    // 导入数据到数据库
    @AuthAccess
    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file) {
        try {
            return carService.importData(file);
        } catch (IOException e) {
            return Result.error("导入失败");
        }
    }
}
